﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;

namespace EC.Admin.Controllers
{
    using EC.Application.Interface;
    using EC.Entity;
    using EC.Entity.Enum;
    using EC.Entity.Query;
    using EC.Entity.Request.BankCard;
    using EC.Entity.Response.BankCard;
    using EC.Util;

    /// <summary>
    /// 银行控制器
    /// </summary>
    public class BankCardController : BaseController
    {
        #region 注入
        /// <summary>
        /// 银行数据接口
        /// </summary>
        private readonly BankCardService _bankCardService;

        /// <summary>
        /// 用户组数据接口
        /// </summary>
        private readonly UserGroupService _userGroupService;

        /// <summary>
        /// 用户组数据接口
        /// </summary>
        private readonly MerchantService _merchantService;
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="labelService"></param>
        public BankCardController(BankCardService bankCardService, UserGroupService userGroupService, MerchantService merchantService, LoginService loginService) : base(loginService)
        {
            _bankCardService = bankCardService;
            _userGroupService = userGroupService;
            _merchantService = merchantService;
        }
        #endregion

        #region 库存卡
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns>视图</returns>
        [AuthFilter(AuthCode.StockCard0001)]
        public IActionResult StockCard()
        {
            return View();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<?></returns>
        [AuthFilter(AuthCode.StockCard0001)]
        public IActionResult StockCardQuery(StockCardQuery query)
        {
            query.UserId = UserId;
            query.CardType = (int)CardType.备用卡;
            var list = _bankCardService.StockCardPager(query);

            return PartialView("_StockCardQuery", list);
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.StockCard0001)]
        public IActionResult StockCardAddOrEdit(int? id)
        {
            ViewBag.CardType = EnumUtil.ToDictionary(typeof(CardType));
            ViewBag.BankCardFrozen = EnumUtil.ToDictionary(typeof(BankCardFrozen));
            ViewBag.BankCardStatus = EnumUtil.ToDictionary(typeof(BankCardStatus));

            var model = new BankCardResponse();
            if (id.HasValue)
            {
                model = _bankCardService.Get(id.Value);
            }

            return View("StockCardEdit", model);
        }

        /// <summary>
        /// 授权
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.StockCard0001)]
        public JsonResult Auth(int id, sbyte status)
        {
            var response = new JResult()
            {
                Success = false
            };

            response = _bankCardService.Auth(id, status);

            return Json(response);
        }

        /// <summary>
        /// 保存、修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [AuthFilter(AuthCode.StockCard0001)]
        public JsonResult SaveStockCard(SaveStockCardRequest request)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                request.UserId = UserId;
                request.MerchantId = MerchantId;
                response = _bankCardService.SaveStockCard(request);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }
        #endregion

        #region 银行卡
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns>视图</returns>
        [AuthFilter(AuthCode.BankCard0001)]
        public IActionResult BankCard()
        {
            return View();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<?></returns>
        [AuthFilter(AuthCode.BankCard0001)]
        public IActionResult BankCardQuery(BankCardQuery query)
        {
            query.UserId = UserId;
            query.CardType = new List<int>() 
            {
                (int)CardType.备用卡,
                (int)CardType.付款卡,
                (int)CardType.收款卡
            };
            //银行卡状态
            query.Status = new List<int>()
            {
                (int)BankCardStatus.启用,
                (int)BankCardStatus.待用,
                (int)BankCardStatus.禁用
            };

            var list = _bankCardService.GetPager(query);

            return PartialView("_BankCardQuery", list);
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.BankCard0002)]
        public IActionResult AddOrEdit(int? id)
        {
            ViewBag.CardType = EnumUtil.ToDictionary(typeof(CardType));
            ViewBag.BankCardFrozen = EnumUtil.ToDictionary(typeof(BankCardFrozen));
            ViewBag.BankCardStatus = EnumUtil.ToDictionary(typeof(BankCardStatus));

            var model = new BankCardResponse();
            if (id.HasValue)
            {
                model = _bankCardService.Get(id.Value);
            }


            var merchantIds = new List<int>() { -1 };
            //获取公司
            var merchant = _merchantService.Get(MerchantId);
            //验证
            if (merchant != null && !string.IsNullOrEmpty(merchant.ProxyId))
            {
                var ids = merchant.ProxyId.Split(',');
                foreach (var item in ids)
                {
                    merchantIds.Add(Convert.ToInt32(item));
                }
            }

            var merchantResponse = _merchantService.GetMerchantByIds(merchantIds);
            if(merchantResponse != null && merchantResponse.Any())
            {
                model.MerchantResponse = merchantResponse;
            }
            return View("Edit", model);
        }


        /// <summary>
        /// 状态
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.BankCard0003)]
        public JsonResult UpdateStatus(int id, sbyte status)
        {
            var response = new JResult()
            {
                Success = false
            };

            response = _bankCardService.Auth(id, status);

            return Json(response);
        }

        /// <summary>
        /// 虚拟机编码
        /// </summary>
        /// <returns>视图</returns>
        [AuthFilter(AuthCode.BankCard0002)]
        public IActionResult VmClientId(int id)
        {
            var model = _bankCardService.Get(id);
            return View(model);
        }

        /// <summary>
        /// 银行卡编辑
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [AuthFilter(AuthCode.BankCard0002)]
        public IActionResult SaveBankCard(SaveBankCardRequest request)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                if (!request.CardType.HasValue)
                {
                    throw new Exception("选择类型.");
                }

                if(request.CardType.Value>0)
                {
                    if (request.MerchantId<=0)
                    {
                        throw new Exception("选择商户.");
                    }
                }

                //状态
                var status = new List<int>()
                {
                    (int)BankCardStatus.启用,
                    (int)BankCardStatus.禁用,
                    (int)BankCardStatus.待用,
                };

                if (!request.Status.HasValue)
                {
                    throw new Exception("选择状态.");
                }

                if (!status.Contains(request.Status.Value))
                {
                    throw new Exception("参数错误.");
                }
                //冻结
                var frozen = new List<int>()
                {
                    (int)BankCardFrozen.有效,
                    (int)BankCardFrozen.冻结
                };

                if (!request.Frozen.HasValue)
                {
                    throw new Exception("选择冻结.");
                }

                if (!frozen.Contains(request.Frozen.Value))
                {
                    throw new Exception("参数错误.");
                }
                response = _bankCardService.SaveBankCard(request);
            }
            catch(Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }

        /// <summary>
        /// 虚拟机编码
        /// </summary>
        /// <returns>视图</returns>
        [AuthFilter(AuthCode.BankCard0002)]
        public IActionResult SaveVmClientId(VmClientIdRequest request)
        {
            var response = _bankCardService.VmClientId(request);

            return Json(response);
        }
        #endregion

        #region 启用银行卡
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns>视图</returns>
        [AuthFilter(AuthCode.EnableBankCard0001)]
        public IActionResult EnableBankCard()
        {
            ViewBag.Type = _merchantService.GetType((int)MerchantType.商户);
            ViewBag.ProxyGroup = _merchantService.GetType((int)MerchantType.卡商);
            return View();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<?></returns>
        [AuthFilter(AuthCode.EnableBankCard0001)]
        public IActionResult EnableBankCardQuery(BankCardQuery query)
        {
            query.UserId = UserId;
            query.CardType = new List<int>()
            {
                (int)CardType.备用卡,
                (int)CardType.付款卡,
                (int)CardType.收款卡
            };
            //银行卡状态
            query.Status = new List<int>()
            {
                (int)BankCardStatus.禁用,
                (int)BankCardStatus.启用,
                (int)BankCardStatus.待用
            };

            var list = _bankCardService.GetSyPager(query);

            return PartialView("_EnableBankCardQuery", list);
        }
        #endregion
    }
}
